<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title th:text="${'管理班级 ' + (classObj != null ? classObj.className : '') + ' 的课程'}"></title>
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <style>
        /* 页面标题和按钮容器 */
        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            flex-wrap: wrap;
            gap: 10px;
        }

        /* 表格样式 */
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border-radius: 8px;
            overflow: hidden;
        }

        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .data-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }

        .data-table tbody tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .data-table tbody tr:hover {
            background-color: #f1f1f1;
        }

        /* 按钮样式 */
        .btn {
            padding: 8px 15px;
            border-radius: 5px;
            text-decoration: none;
            color: white;
            cursor: pointer;
            border: none;
            font-size: 0.9em;
            margin-right: 5px;
        }

        .btn-primary { background-color: #007bff; }
        .btn-primary:hover { background-color: #0056b3; }
        .btn-success { background-color: #28a745; }
        .btn-success:hover { background-color: #218838; }
        .btn-info { background-color: #17a2b8; }
        .btn-info:hover { background-color: #138496; }
        .btn-warning { background-color: #ffc107; }
        .btn-warning:hover { background-color: #e0a800; }
        .btn-danger { background-color: #dc3545; }
        .btn-danger:hover { background-color: #c82333; }

        .action-buttons {
            white-space: nowrap;
        }

        /* 表单容器 */
        .form-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-group select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .form-group button {
            width: auto;
            padding: 10px 20px;
            border-radius: 5px;
        }

        /* 消息提示样式 */
        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid transparent;
            border-radius: 4px;
        }

        .alert-success {
            color: #155724;
            background-color: #d4edda;
            border-color: #c3e6cb;
        }

        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }
    </style>
</head>
<body>
<div th:replace="~{header :: header}"></div>
<div th:replace="~{sidebar :: sidebar}"></div>

<div class="main-content">
    <div class="page-header">
        <h1 th:text="${'管理班级 ' + (classObj != null ? classObj.className : '') + ' 的课程'}"></h1>
        <a th:href="@{/class/manage}" class="btn btn-primary">返回班级列表</a>
    </div>

    <div th:if="${successMessage}" class="alert alert-success">
        <p th:text="${successMessage}"></p>
    </div>
    <div th:if="${errorMessage}" class="alert alert-danger">
        <p th:text="${errorMessage}"></p>
    </div>

    <div class="form-container">
        <h3>向班级 <span th:text="${classObj.className}"></span> 添加课程</h3>
        <form th:action="@{/class/{classId}/courses/add(classId=${classId})}" method="post">
            <div class="form-group">
                <label for="courseSelect">选择课程：</label>
                <select id="courseSelect" name="courseId" class="form-control" required>
                    <option value="">请选择一个课程</option>
                    <option th:each="course : ${coursesToAdd}"  th:value="${course.id}"
                            th:text="${course.couName}">
                        课程名称
                    </option>
                </select>
            </div>
            <button type="submit" class="btn btn-success">添加课程</button>
        </form>
        <div th:unless="${!#lists.isEmpty(coursesToAdd)}" class="alert alert-info" style="margin-top: 15px;">
            <p>目前没有其他可用课程可以添加到该班级。</p>
        </div>
    </div>

    <div class="courses-in-class-list">
        <h4>班级 <span th:text="${classObj.className}"></span> 已关联课程</h4>
        <div th:if="${!#lists.isEmpty(coursesInClass)}">
            <table class="data-table">
                <thead>
                <tr>
                    <th>课程ID</th>
                    <th>课程名称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="course : ${coursesInClass}">
                    <td th:text="${course.id}"></td>
                    <td th:text="${course.couName}"></td>
                    <td>
                        <form th:action="@{/class/{classId}/courses/delete/{courseId}(classId=${classId}, courseId=${course.id})}" method="post" style="display: inline;">
                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('确定要从该班级中移除此课程吗？');">移除</button>
                        </form>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <div th:unless="${!#lists.isEmpty(coursesInClass)}" class="alert alert-info">
            <p>该班级目前没有关联任何课程。</p>
        </div>
    </div>
</div>
<div th:replace="~{footer :: footer}"></div>
</body>
</html>